package com.c.menu;

import com.alibaba.fastjson.JSON;
import com.c.menu.model.Button;
import com.c.menu.model.Menu;
import com.c.menu.model.ViewButton;
import com.c.util.HttpUtil;

import java.util.Map;

/**
 * @author bysun
 */
public class MenuApi {

    public final static String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
    public final static String GET_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";
    public final static String DEL_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";

    public static String createMenu(String access_token, String menu) {
        String url = CREATE_MENU_URL.replace("ACCESS_TOKEN", access_token);
        Map<String, Object> map = JSON.parseObject(menu, Map.class);
        return HttpUtil.post(url, map, 1);
    }

    public static String createMenu(String access_token, Menu menu) {
        return createMenu(access_token, JSON.toJSONString(menu));
    }

    public static String getMenu(String access_token) {
        String url = GET_MENU_URL.replace("ACCESS_TOKEN", access_token);
        return HttpUtil.get(url);
    }

    public static String delMenu(String access_token) {
        String url = DEL_MENU_URL.replace("ACCESS_TOKEN", access_token);
        return HttpUtil.get(url);
    }


//    public static String

    public static void main(String[] args) {
        Menu menu = new Menu();

        ViewButton viewButton = new ViewButton();
        viewButton.setName("测试下单");
        viewButton.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf7bac7d05b02d79c&redirect_uri=http%3a%2f%2fproducttest.doulaibao.com.cn%2fproducts%2fdlb_wx%2f%23%2f&response_type=code&scope=snsapi_base#wechat_redirect");
        viewButton.setType("view");

        Button button = new Button();
        button.setName("菜单");

        ViewButton btn1 = new ViewButton();
        btn1.setName("理财查询");
        btn1.setUrl("http://producttest.doulaibao.com.cn/products/10003/#/login");
        btn1.setType("view");

        ViewButton btn2 = new ViewButton();
        btn2.setName("车险查询");
        btn2.setUrl("http://producttest.doulaibao.com.cn/products/10001/");
        btn2.setType("view");

        ViewButton btn3 = new ViewButton();
        btn3.setName("个险查询");
        btn3.setUrl("http://producttest.doulaibao.com.cn/products/order_query/#/insIndex");
        btn3.setType("view");

        ViewButton btn4 = new ViewButton();
        btn4.setName("个人中心");
        btn4.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf7bac7d05b02d79c&redirect_uri=http%3a%2f%2fproducttest.doulaibao.com.cn%2fproducts%2forder_query%2f%23%2fuserCenter&response_type=code&connect_redirect=1&scope=snsapi_userinfo#wechat_redirect");
        btn4.setType("view");

        ViewButton btn5 = new ViewButton();
        btn5.setName("新个险查询");
        btn5.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf7bac7d05b02d79c&redirect_uri=http%3a%2f%2fproducttest.doulaibao.com.cn%2fproducts%2forder_query%2f%23%2forderInquiry&response_type=code&connect_redirect=1&scope=snsapi_userinfo#wechat_redirect");
        btn5.setType("view");

        button.setSub_button(new Button[]{btn1, btn2, btn3, btn4, btn5});

        menu.setButton(new Button[]{viewButton, button});

        System.out.println(JSON.toJSON(menu));

//        String access_token = WechaUtil.getAccessToken();

        createMenu("10_oOde_VwEstxr_RXFv1fCeFndf945ucc21iX3xRN2hcL5c40iC0iP_ZC_j87iucT5oIhDbtgZ8Y7OoGCP0DH1o8iDGAOQ3deFS5iE-9w21Qd2mvnKQpzMb6gZjlomdLcsOS8uONs7Op_zr8zwJZSgAIAEUN", menu);
    }
}
